﻿namespace ExcelReader.Helper
{
    using System.ComponentModel;
    using System;

    internal static class ConverterHelper
    {
        /* OpenXML SDK stores datetime values as day starting from Jan 1, 1900
         */
        internal static object Convert(this string value, Type type)
        {
            var columnType = TypeDescriptor.GetConverter(type);
            if (columnType is DateTimeConverter)
            {
                double convertedValue;
                if (double.TryParse(value, out convertedValue))
                {
                    if (convertedValue >= Constant.List.OADates.Min && convertedValue <= Constant.List.OADates.Max)
                        return DateTime.FromOADate(convertedValue);
                }
                return null;
            }
            return TypeDescriptor.GetConverter(type).ConvertFromString(value);
        }
    }
}
